package com.lianbang.staff.module.main

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.Menu
import android.view.MenuItem
import cn.ubest.mvvmcore.base.BaseListActivity
import cn.ubest.mvvmcore.utils.TimeUtils
import cn.ubest.mvvmcore.widget.ListDivider
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lianbang.staff.R
import com.lianbang.staff.adapter.OrderDetailListAdapter
import com.lianbang.staff.bean.CartBean
import com.lianbang.staff.bean.ShopOrderDetailBean
import com.lianbang.staff.databinding.ActivityOrderDetailBinding
import com.lianbang.staff.dialog.ShipConfirmDialog
import com.lianbang.staff.vm.OrderVM
import kotlinx.android.synthetic.main.activity_order_detail.*
import kotlinx.android.synthetic.main.activity_shop_detail.tv_toolbar_title

class OrderDetailActivity : BaseListActivity<OrderVM, ActivityOrderDetailBinding, CartBean>() {

    companion object {
        fun start(context: Context?, id: Int, title: String, phone: String) {
            context?.also {
                val starter = Intent(it, OrderDetailActivity::class.java)
                if (it !is Activity) {
                    starter.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                starter.putExtra("id", id)
                starter.putExtra("title", title)
                starter.putExtra("phone", title)
                it.startActivity(starter)
            }

        }
    }

    private var mDetailBean: ShopOrderDetailBean? = null
    private var mDialog: ShipConfirmDialog? = null
    override fun initLayout(): Int {
        return R.layout.activity_order_detail

    }

    override fun initData(intent: Intent) {
        super.initData(intent)
    }

    override fun initView() {
        super.initView()
        btn_operate.setOnClickListener(getDoubleClickListener {
            mDetailBean?.also {
                if (it.status == OrderFragment.ORDER_ADDED
                    || it.status == OrderFragment.ORDER_HANDLED
                    || it.status == OrderFragment.ORDER_RETURN
                ) {
                    //改变订单状态
                    mDialog = ShipConfirmDialog.newInstance(it)
                    addFragment(mDialog!!)
                }
            }
        })
        mRvList?.addItemDecoration(
            ListDivider
                .Build()
                .setMarginLeft(R.dimen.dp_20)
                .setMarinRight(R.dimen.dp_20)
                .setColor("#F3F5F9")
                .build()
        )
    }

    override fun loadData() {
        mViewModel.getOrderDetail(intent.getIntExtra("id", -1))
            .observe(this, getObserver {
                mDetailBean = it
                tv_toolbar_title.text = intent.getStringExtra("title")
                tv_order_number.text = "订单编号：${it.order_id}"
                tv_date.text = TimeUtils.formatTime(it.add_time * 1000, "yyyy/MM/dd")
                tv_category_number.text = "${it.cart_num}"
                tv_number.text = "${it.total_num}"
                tv_total_money.text = it.total_price

                tv_state.text = when (it.status) {
                    OrderFragment.ORDER_ADDED -> "新增订单"
                    OrderFragment.ORDER_HANDLED -> "已处理"
                    OrderFragment.ORDER_RETURN -> "退货/售后"
                    OrderFragment.ORDER_FINISHED -> "已完成"
                    else -> ""
                }
                tv_operate.text = when (it.status) {
                    OrderFragment.ORDER_ADDED -> "处理订单"
                    OrderFragment.ORDER_HANDLED -> "完成"
                    OrderFragment.ORDER_RETURN -> "完成"
                    OrderFragment.ORDER_FINISHED -> "暂无操作"
                    else -> ""
                }
                mListAdapter.replaceData(it.cart_info)
            })


    }

    override fun initAdapter(): BaseQuickAdapter<CartBean, *> {
        return OrderDetailListAdapter()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_order_detail, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.btn_call -> {
                intent.getStringExtra("phone")?.also {
                    val intent = Intent(
                        Intent.ACTION_CALL,
                        Uri.parse("tel:$it")
                    )
                    startActivity(intent)
                }
            }
        }
        return super.onOptionsItemSelected(item)
    }

    fun orderStateChanged() {
        mDetailBean?.apply {
            val state = when (status) {
                OrderFragment.ORDER_ADDED -> OrderFragment.ORDER_HANDLED
                else -> OrderFragment.ORDER_FINISHED
            }
            mViewModel.confirmShopOrder(id, state, total_price.toFloat())
                .observe(this@OrderDetailActivity, getObserver {
                    mDialog?.also {
                        removeFragment(it)
                    }
                    loadData()
                })
        }
    }
}
